package org.classg.msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.classg.msm.service.MsmService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {

    // 根据手机号发送短信验证码
    @Override
    public Boolean sendMsmCode(String phone, Map<String, Object> param) {
        if (StringUtils.isEmpty(phone)) {return false;}
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI4GBeVvB7mD8qjyMTk4hy", "1lBCF12isdSdduU8BCxlrPJrpUXWMy");
        IAcsClient client = new DefaultAcsClient(profile);
        // 设置相关固定的参数
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        // 设置发送相关的参数
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "个人极越在线教育系统");
        request.putQueryParameter("TemplateCode", "SMS_192380092");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));// 验证码数据，转换json数据传递

        try {
            // 验证码数据，转换json数据传递
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }
}
